<template>

  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/heima.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">后退
      </el-button>
    </el-header>
    <el-container>
      <!--侧边栏-->
      <el-aside :width="isCollapse?'64px':'200px'">
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409EFF" unique-opened :collapse="isCollapse"  :collapse-transition="false" :router="true"  :default-active="isActived">
          <div class="toggle-button" @click="taggleCollapse">|||</div>
          <!--一级菜单-->
          <el-submenu :index="item.id+''" v-for="item  in items" :key="item.id">
            <!--一级菜单的模板区域-->
            <template slot="title">
              <i :class="iconsObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!--二级菜单的模板区域-->
            <el-menu-item :index="'/'+subItem.path" v-for="subItem in item.children" :key="subItem.id" @click="getIndex(subItem.path)">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>

          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>

</template>

<script>
export default {
  name: 'Home',
  data() {
    return {
      items: [],
      iconsObj: {
        '125': 'iconfont icon-user',
        '103': 'iconfont icon-tijikongjian',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      isCollapse: false,
      isActived:''
    }
  },
  created() {
    this.getMenuList()
    this.isActived=window.sessionStorage.getItem('activedIndex')
  },
  methods: {
    logout() {
      window.sessionStorage.clear();
      this.$router.push("/login")
    },
    async getMenuList() {
      const {data: res} = await this.$http.get('/menus')
      console.log(res)
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg);
      } else {
        this.items = res.data
      }
    },
    taggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    getIndex(str){
      window.sessionStorage.setItem('activedIndex','/'+str);
      this.isActived=window.sessionStorage.getItem('activedIndex');
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
  color: #333;
  text-align: center;
  line-height: 60px;
}

.el-header {
  height: 20%;
  display: flex;
  justify-content: space-between;
  padding-left: 1px;

  > div {
    display: flex;
    align-items: center;
  }

  span {
    color: white;
    font-size: 20px;
    margin-left: 15px;
  }
}

.el-aside {
  background-color: #333744;
  color: #333;
  text-align: center;
  line-height: 200px;

  .el-menu {
    height: 100%;
    border-right: none;
  }

  .el-submenu .el-menu-item {
    padding: 0px;
  }
}

.el-main {
  background-color: #EAEDF1;
  color: #333;
  text-align: center;
  line-height: 160px;
}

.el-container.home-container {
  height: 100%;
}

body > .el-container {
  margin-bottom: 40px;
}

.el-container:nth-child(5) .el-aside,
.el-container:nth-child(6) .el-aside {
  line-height: 260px;
}

.el-container:nth-child(7) .el-aside {
  line-height: 320px;
}

.iconfont:before {
  margin-right: 10px;
}

.toggle-button {
  background-color: #4A5064;
  line-height: 24px;
  font-size: 10px;
  color: white;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
